<template>
  <Header :title="'编辑地址'"></Header>
  <div class="adddressAdd">
    <van-address-edit
      :area-list="state.areaList"
      :address-info="state.addressInfo"
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>
<script setup>
import Header from "@/components/SHeader.vue";
import { onMounted, reactive } from "vue";
import { tdist } from "@/util/city";
import { addressInfo, addressEdit, addressAdd } from "@/api/address";
import { useRoute, useRouter } from "vue-router";
import { toast } from "@/util/toast";
const route = useRoute();
const state = reactive({
  type: "add",
  areaList: {},
  addressId: 0,
  from: "",
  addressInfo: {},
});
const router = useRouter();
onMounted(async () => {
  let _province_list = {};
  let _city_list = {};
  let _county_list = {};
  tdist.getLev1().forEach((p) => {
    _province_list[p.id] = p.text;

    tdist.getLev2(p.id).forEach((c) => {
      _city_list[c.id] = c.text;

      tdist.getLev3(c.id).forEach((q) => (_county_list[q.id] = q.text));
    });
  });
  state.areaList.province_list = _province_list;
  state.areaList.city_list = _city_list;
  state.areaList.county_list = _county_list;

  const { addressId, type, from } = route.query;
  state.type = type;
  state.addressId = addressId;
  state.from = from;
  // if (type === "edit") {
  //   const { data: addressDetail } = await getAddressDetail(addressId);
  //   console.log(addressDetail);
  //   let _areaCode = "";
  //   const province = tdist.getLev1();
  //   Object.entries(state.areaList.county_list).forEach(([id, text]) => {
  //     // 先找出当前对应的区
  //     if (text == addressDetail.regionName) {
  //       // 找到区对应的几个省份
  //       const provinceIndex = province.findIndex(
  //         (item) => item.id.substr(0, 2) == id.substr(0, 2)
  //       );
  //       // 找到区对应的几个市区
  //       // eslint-disable-next-line no-unused-vars
  //       const cityItem = Object.entries(state.areaList.city_list).filter(
  //         ([cityId, cityName]) => cityId.substr(0, 4) == id.substr(0, 4)
  //       )[0];
  //       // 对比找到的省份和接口返回的省份是否相等，因为有一些区会重名
  //       if (
  //         province[provinceIndex].text == addressDetail.provinceName &&
  //         cityItem[1] == addressDetail.cityName
  //       ) {
  //         _areaCode = id;
  //       }
  //     }
  //   });
  // }
  if (route.query.type === "edit") {
    getAddressInfo();
  }
});
const getAddressInfo = async () => {
  const { data } = await addressInfo(route.query.address_id);
  const res = data.res[0];
  console.log(res);
  state.addressInfo = {
    id: res.address_id,
    name: res.username,
    tel: res.phone_number,
    province: res.province,
    city: res.city,
    county: res.county,
    addressDetail: res.addressDetail,
    areaCode: res.areaCode,
    isDefault: !!res.isDefault,
  };
};
// 保存
const onSave = async (item) => {
  item.isDefault = Number(item.isDefault);
  if (route.query.type === "edit") {
    await addressEdit(item).then((res) => {
      toast("success", "保存成功", 500);
      router.go(-1);
    });
  } else {
    await addressAdd(item).then((res) => {
      toast("success", "保存成功", 500);
      router.go(-1);
    });
  }
};
</script>
<style lang="scss">
.van-address-edit__default {
  text-align: left;
}
</style>
